<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="org.apache.commons.fileupload.FileUpload"%>
<%@page import="java.sql.ResultSet"%>
<%@ page import="java.util.Date" language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page session="true" %>
<%@ page contentType="text/html; charset=UTF-8" import="com.mysql.jdbc.*"%>
<%@ page contentType="text/html; charset=UTF-8" import="java.sql.Connection"%>
<%@ page contentType="text/html; charset=UTF-8" import="java.sql.DriverManager"%>
<%@ page contentType="text/html; charset=UTF-8" import="java.sql.Statement"%>
<%@ page contentType="text/html; charset=UTF-8" import="java.util.*"%>
<%@ page import = "org.apache.commons.*" %>
<%@ page import = "org.apache.commons.fileupload.DiskFileUpload" %>

<%@ page import = "java.io.File" %>
<%@ page import = "java.io.BufferedInputStream" %>
<%@ page import = "java.io.BufferedOutputStream" %>
<%@ page import = "java.io.FileOutputStream" %>
<%@ page import = "java.io.IOException" %>


<%!
	/**
	* 등록된 프로젝트에 가입합니다.
	**/ 
	public int projectJoin(String _strProjectId, String _strUserId, int _nLevel){

		Connection conn = null;
	    Statement stmt = null;
	 	int ret = 0;
	 	
	 	
	    try
	    {
	    	Class.forName("com.mysql.jdbc.Driver").newInstance(); 
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mimo" ,"root" ,"d0o9o84");
	        
	        stmt = conn.createStatement();
	        
	        ResultSet result = stmt.executeQuery("select * from projectrels where projectId='" + _strProjectId + "' AND userId='" + _strUserId + "';");
	        
	        if(result.next() == true){
	        	stmt.execute("update projectrels set userLevel=" + String.valueOf(_nLevel) + " where projectId='" + _strProjectId + "' AND userId='" + _strUserId + "';");
	        }else{
	        	stmt.execute("insert into projectrels(projectId, userId, userLevel) values('" + _strProjectId + "', '" + _strUserId + "', '" + String.valueOf(_nLevel) + "');");
	        }
	        stmt.close();
	        conn.close();
	    }
	    catch( Exception ex )
	    {
	    	ex.printStackTrace();
	    	ret = -1;
	    }
	
		return ret; 
	}

	/**
	* 프로젝트를 등록합니다.
	**/
	public int createProject(String _strProjectId, String _strProjectName, String _strDescript, String _strLeader, StringBuffer _strbufMessage){
	
		Connection conn = null;
	    Statement stmt = null;
	 	int ret = 0;
	 	
	 	
	    try
	    {
	    	Class.forName("com.mysql.jdbc.Driver").newInstance(); 
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mimo" ,"root" ,"d0o9o84");
	        
	        stmt = conn.createStatement();
	        
	        ResultSet result = stmt.executeQuery("select * from project where projectId='" + _strProjectId + "';");
	        if(result.next() == true){
		        stmt.close();
		        conn.close();
		        
		        _strbufMessage.append("already exist project ID.");
		        return -1;
	        }
			
	        if(_strDescript == null){
	        	_strDescript = "";
	        }
	        
	        stmt.execute("insert into project(projectId, projectName, leader, description) values('" + _strProjectId + "', '" + _strProjectName + "', '" + _strLeader + "', '" + _strDescript + "');");
	        
	        stmt.close();
	        conn.close();
	    }
	    catch( Exception ex )
	    {
	    	ex.printStackTrace();
	    	ret = -1;
	    }
	
		return ret; 
		
	}
%>
<% 
	session = request.getSession(true);
	String strId = (String)session.getAttribute("id");
	if(strId == null){
		response.sendRedirect("login.jsp");
	}
%>
<%
	if (FileUpload.isMultipartContent(request)) {
		String temporaryDir = "E:/temp";
		DiskFileUpload fileUpload = new DiskFileUpload();
		fileUpload.setRepositoryPath(temporaryDir);
		fileUpload.setSizeMax(1024*1024 * 10);//10MB
		fileUpload.setSizeThreshold(1024*100); 
		
		String strProjectId = null;
		String strProjectName = null;
		String strProjectDesription = null;
		
		List fileItemList = fileUpload.parseRequest(request);
        for (int i = 0 ; i < fileItemList.size() ; i++) {
            FileItem fileItem = (FileItem) fileItemList.get(i);
            if (fileItem.isFormField()) {
            	if(fileItem.getFieldName().equals("projectId") == true){
            		strProjectId = fileItem.getString();
            	}else if(fileItem.getFieldName().equals("projectName") == true){
            		strProjectName = fileItem.getString();
            	}else if(fileItem.getFieldName().equals("projectDescription") == true){
            		strProjectDesription = fileItem.getString();
            	}

            	//폼 파라미터: fileItem.getFieldName() = fileItem.getString() <br />

            }else{
            	out.println("file name:" + fileItem.getName() +":");
            	
            	File f = new File("E:/temp", "a.png");
            	fileItem.write(f);
            	
            }
        }
        
        
        StringBuffer strMsg = new StringBuffer();
        int ret = -1;
        if((strProjectId != null) && (strProjectName != null)){
        	ret = createProject(strProjectId, strProjectName, strProjectDesription, strId , strMsg);
        }
		
		if(ret == -1){
			response.sendRedirect("selectProject.jsp?msg=" + strMsg.toString());
		}else{
			session.setAttribute("projectId", strProjectId);
			projectJoin(strProjectId, strId, 0);
			response.sendRedirect("projectNavigate.jsp?projectId="+strProjectId);
		}
		
	}

%>